package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.mapper.AlbumMapper;
import cn.tedu.csmall.product.mapper.PictureMapper;
import cn.tedu.csmall.product.pojo.dto.PictureAddNewDTO;
import cn.tedu.csmall.product.pojo.entity.Picture;
import cn.tedu.csmall.product.pojo.vo.AlbumStandardVO;
import cn.tedu.csmall.product.pojo.vo.PictureStandardVO;
import cn.tedu.csmall.product.service.IPictureService;
import cn.tedu.csmall.product.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class PictureServiceImpl implements IPictureService {

    @Autowired
    PictureMapper pictureMapper;
    @Autowired
    AlbumMapper albumMapper;

    @Override
    public void addNew(PictureAddNewDTO pictureAddNewDTO) {
        log.debug("开始处理【添加图片】业务，新图片：{}", pictureAddNewDTO);
        AlbumStandardVO queryResult = albumMapper.getStandardById(pictureAddNewDTO.getAlbumId());
        //存在相册
        if(queryResult == null){
            String message = "添加图片失败，尝试添加的相册不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        Picture picture = new Picture();
        BeanUtils.copyProperties(pictureAddNewDTO, picture);
        pictureMapper.insert(picture);
    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理【删除图片】业务，ID：{}", id);
        PictureStandardVO queryResult = pictureMapper.getStandardById(id);
        if(queryResult == null){
            String message = "删除图片失败，尝试访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        pictureMapper.deleteById(id);
    }

}
